// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"bytes"
	"context"
	"go/format"
	"strings"

	"github.com/bufbuild/buf/private/pkg/app"
	"github.com/bufbuild/buf/private/pkg/app/appcmd"
	"github.com/bufbuild/buf/private/pkg/spdx"
	"github.com/spf13/pflag"
)

const (
	programName = "spdx-go-data"

	pkgFlagName = "package"
)

func main() {
	appcmd.Main(context.Background(), newCommand())
}

func newCommand() *appcmd.Command {
	flags := newFlags()
	return &appcmd.Command{
		Use:  programName,
		Args: appcmd.NoArgs,
		Run: func(ctx context.Context, container app.Container) error {
			return run(ctx, container, flags)
		},
		BindFlags: flags.Bind,
	}
}

type flags struct {
	Pkg string
}

func newFlags() *flags {
	return &flags{}
}

func (f *flags) Bind(flagSet *pflag.FlagSet) {
	flagSet.StringVar(
		&f.Pkg,
		pkgFlagName,
		"",
		"The name of the generated package.",
	)
	_ = appcmd.MarkFlagRequired(flagSet, pkgFlagName)
}

func run(ctx context.Context, container app.Container, flags *flags) error {
	licenseInfos, err := spdx.GetLicenseInfos(ctx)
	if err != nil {
		return err
	}
	data, err := getGolangFileData(ctx, flags.Pkg, licenseInfos)
	if err != nil {
		return err
	}
	_, err = container.Stdout().Write(data)
	return err
}

func getGolangFileData(
	ctx context.Context,
	pkg string,
	licenseInfos []*spdx.LicenseInfo,
) ([]byte, error) {
	buffer := bytes.NewBuffer(nil)
	_, _ = buffer.WriteString(`// Code generated by ` + programName + `. DO NOT EDIT.

package ` + pkg + `

import "strings"

// LicenseInfo is SPDX license information.
//
// See https://spdx.org/licenses.
type LicenseInfo interface {
	// The SPDX identifier.
	//
	// Case-sensitive.
	ID() string
}

// GetLicenseInfo gets the LicenseInfo for the id.
//
// The parameter id is not case-sensitive.
func GetLicenseInfo(id string) (LicenseInfo, bool) {
	licenseInfo, ok := lowercaseIDToLicenseInfo[strings.ToLower(id)]
	return licenseInfo, ok
}

var lowercaseIDToLicenseInfo = map[string]*licenseInfo{
`)

	for _, licenseInfo := range licenseInfos {
		_, _ = buffer.WriteString(`"` + strings.ToLower(licenseInfo.ID) + `": &licenseInfo{
id: "` + licenseInfo.ID + `",
},
`)
	}

	_, _ = buffer.WriteString(`}

type licenseInfo struct {
	id string
}

func (l *licenseInfo) ID() string {
	return l.id
}`)
	return format.Source(buffer.Bytes())
}
